﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text.RegularExpressions;

namespace Hongsh {
    // Token: 0x02000016 RID: 22
    public static class RsaExtends {
        // Token: 0x06000091 RID: 145 RVA: 0x00006FEC File Offset: 0x000051EC
        private static int GetIntegerSize(BinaryReader binr) {
            byte b = binr.ReadByte();
            int result;
            if (b != 2) {
                result = 0;
            }
            else {
                b = binr.ReadByte();
                int num;
                if (b == 129) {
                    num = (int)binr.ReadByte();
                }
                else if (b == 130) {
                    byte b2 = binr.ReadByte();
                    byte b3 = binr.ReadByte();
                    byte[] array = new byte[4];
                    array[0] = b3;
                    array[1] = b2;
                    byte[] value = array;
                    num = BitConverter.ToInt32(value, 0);
                }
                else {
                    num = (int)b;
                }
                while (binr.ReadByte() == 0) {
                    num--;
                }
                binr.BaseStream.Seek(-1L, SeekOrigin.Current);
                result = num;
            }
            return result;
        }

        // Token: 0x06000092 RID: 146 RVA: 0x0000708C File Offset: 0x0000528C
        private static bool CompareBytearrays(byte[] a, byte[] b) {
            bool result;
            if (a.Length != b.Length) {
                result = false;
            }
            else {
                int num = 0;
                foreach (byte b2 in a) {
                    if (b2 != b[num]) {
                        return false;
                    }
                    num++;
                }
                result = true;
            }
            return result;
        }

        // Token: 0x06000093 RID: 147 RVA: 0x000070DC File Offset: 0x000052DC
        public static RSACryptoServiceProvider FromPKCS1RSAPublicKey(byte[] publickey) {
            byte[] b = new byte[]
            {
                48,
                13,
                6,
                9,
                42,
                134,
                72,
                134,
                247,
                13,
                1,
                1,
                1,
                5,
                0
            };
            MemoryStream input = new MemoryStream(publickey);
            BinaryReader binaryReader = new BinaryReader(input);
            RSACryptoServiceProvider result;
            try {
                ushort num = binaryReader.ReadUInt16();
                if (num == 33072) {
                    binaryReader.ReadByte();
                }
                else {
                    if (num != 33328) {
                        return null;
                    }
                    binaryReader.ReadInt16();
                }
                byte[] a = binaryReader.ReadBytes(15);
                if (!RsaExtends.CompareBytearrays(a, b)) {
                    result = null;
                }
                else {
                    num = binaryReader.ReadUInt16();
                    if (num == 33027) {
                        binaryReader.ReadByte();
                    }
                    else {
                        if (num != 33283) {
                            return null;
                        }
                        binaryReader.ReadInt16();
                    }
                    byte b2 = binaryReader.ReadByte();
                    if (b2 > 0) {
                        result = null;
                    }
                    else {
                        num = binaryReader.ReadUInt16();
                        if (num == 33072) {
                            binaryReader.ReadByte();
                        }
                        else {
                            if (num != 33328) {
                                return null;
                            }
                            binaryReader.ReadInt16();
                        }
                        num = binaryReader.ReadUInt16();
                        byte b3 = 0;
                        byte b4;
                        if (num == 33026) {
                            b4 = binaryReader.ReadByte();
                        }
                        else {
                            if (num != 33282) {
                                return null;
                            }
                            b3 = binaryReader.ReadByte();
                            b4 = binaryReader.ReadByte();
                        }
                        byte[] array = new byte[4];
                        array[0] = b4;
                        array[1] = b3;
                        byte[] value = array;
                        int num2 = BitConverter.ToInt32(value, 0);
                        byte b5 = binaryReader.ReadByte();
                        binaryReader.BaseStream.Seek(-1L, SeekOrigin.Current);
                        if (b5 == 0) {
                            binaryReader.ReadByte();
                            num2--;
                        }
                        byte[] modulus = binaryReader.ReadBytes(num2);
                        if (binaryReader.ReadByte() != 2) {
                            result = null;
                        }
                        else {
                            int count = (int)binaryReader.ReadByte();
                            byte[] exponent = binaryReader.ReadBytes(count);
                            RSACryptoServiceProvider rsacryptoServiceProvider = new RSACryptoServiceProvider();
                            RSAParameters parameters = new RSAParameters {
                                Modulus = modulus,
                                Exponent = exponent
                            };
                            rsacryptoServiceProvider.ImportParameters(parameters);
                            result = rsacryptoServiceProvider;
                        }
                    }
                }
            }
            catch {
                result = null;
            }
            finally {
                binaryReader.Close();
            }
            return result;
        }

        // Token: 0x06000094 RID: 148 RVA: 0x00007310 File Offset: 0x00005510
        public static RSACryptoServiceProvider FromPKCS1RSAPrivateKey(byte[] privkey) {
            MemoryStream input = new MemoryStream(privkey);
            BinaryReader binaryReader = new BinaryReader(input);
            RSACryptoServiceProvider result;
            try {
                ushort num = binaryReader.ReadUInt16();
                if (num == 33072) {
                    binaryReader.ReadByte();
                }
                else {
                    if (num != 33328) {
                        return null;
                    }
                    binaryReader.ReadInt16();
                }
                num = binaryReader.ReadUInt16();
                if (num != 258) {
                    result = null;
                }
                else {
                    byte b = binaryReader.ReadByte();
                    if (b > 0) {
                        result = null;
                    }
                    else {
                        int integerSize = RsaExtends.GetIntegerSize(binaryReader);
                        byte[] modulus = binaryReader.ReadBytes(integerSize);
                        integerSize = RsaExtends.GetIntegerSize(binaryReader);
                        byte[] exponent = binaryReader.ReadBytes(integerSize);
                        integerSize = RsaExtends.GetIntegerSize(binaryReader);
                        byte[] d = binaryReader.ReadBytes(integerSize);
                        integerSize = RsaExtends.GetIntegerSize(binaryReader);
                        byte[] p = binaryReader.ReadBytes(integerSize);
                        integerSize = RsaExtends.GetIntegerSize(binaryReader);
                        byte[] q = binaryReader.ReadBytes(integerSize);
                        integerSize = RsaExtends.GetIntegerSize(binaryReader);
                        byte[] dp = binaryReader.ReadBytes(integerSize);
                        integerSize = RsaExtends.GetIntegerSize(binaryReader);
                        byte[] dq = binaryReader.ReadBytes(integerSize);
                        integerSize = RsaExtends.GetIntegerSize(binaryReader);
                        byte[] inverseQ = binaryReader.ReadBytes(integerSize);
                        RSACryptoServiceProvider rsacryptoServiceProvider = new RSACryptoServiceProvider();
                        rsacryptoServiceProvider.ImportParameters(new RSAParameters {
                            Modulus = modulus,
                            Exponent = exponent,
                            D = d,
                            P = p,
                            Q = q,
                            DP = dp,
                            DQ = dq,
                            InverseQ = inverseQ
                        });
                        result = rsacryptoServiceProvider;
                    }
                }
            }
            catch {
                result = null;
            }
            finally {
                binaryReader.Close();
            }
            return result;
        }

        // Token: 0x06000095 RID: 149 RVA: 0x000074D0 File Offset: 0x000056D0
        public static RSACryptoServiceProvider FromPemString(string pem_string) {
            Match match = Regex.Match(pem_string, RsaExtends.FromPemStringRegex);
            if (match.Length >= 0 && !string.IsNullOrEmpty(match.Groups["r"].Value)) {
                if (match.Groups["p"].Value == "PUBLIC") {
                    return RsaExtends.FromPKCS1RSAPublicKey(Convert.FromBase64String(match.Groups["k"].Value));
                }
                if (match.Groups["p"].Value == "PRIVATE") {
                    return RsaExtends.FromPKCS1RSAPrivateKey(Convert.FromBase64String(match.Groups["k"].Value));
                }
            }
            return null;
        }

        // Token: 0x06000096 RID: 150 RVA: 0x000075A0 File Offset: 0x000057A0
        private static void EncodeLength(BinaryWriter stream, int length) {
            if (length < 0) {
                throw new ArgumentOutOfRangeException("length", "Length must be non-negative");
            }
            if (length < 128) {
                stream.Write((byte)length);
            }
            else {
                int i = length;
                int num = 0;
                while (i > 0) {
                    i >>= 8;
                    num++;
                }
                stream.Write((byte)(num | 128));
                for (int j = num - 1; j >= 0; j--) {
                    stream.Write((byte)(length >> 8 * j & 255));
                }
            }
        }

        // Token: 0x06000097 RID: 151 RVA: 0x00007624 File Offset: 0x00005824
        private static void EncodeIntegerBigEndian(BinaryWriter stream, byte[] value, bool forceUnsigned = true) {
            stream.Write(2);
            int num = 0;
            int num2 = 0;
            while (num2 < value.Length && value[num2] <= 0) {
                num++;
                num2++;
            }
            if (value.Length - num == 0) {
                RsaExtends.EncodeLength(stream, 1);
                stream.Write(0);
            }
            else {
                if (forceUnsigned && value[num] > 127) {
                    RsaExtends.EncodeLength(stream, value.Length - num + 1);
                    stream.Write(0);
                }
                else {
                    RsaExtends.EncodeLength(stream, value.Length - num);
                }
                for (int i = num; i < value.Length; i++) {
                    stream.Write(value[i]);
                }
            }
        }

        // Token: 0x06000098 RID: 152 RVA: 0x000076BC File Offset: 0x000058BC
        public static string ToXmlString(this RSA rsa, bool includePrivateParameters) {
            RSAParameters rsaparameters = rsa.ExportParameters(includePrivateParameters);
            string result;
            if (includePrivateParameters) {
                result = string.Format("<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent><P>{2}</P><Q>{3}</Q><DP>{4}</DP><DQ>{5}</DQ><InverseQ>{6}</InverseQ><D>{7}</D></RSAKeyValue>", new object[]
                {
                    Convert.ToBase64String(rsaparameters.Modulus),
                    Convert.ToBase64String(rsaparameters.Exponent),
                    Convert.ToBase64String(rsaparameters.P),
                    Convert.ToBase64String(rsaparameters.Q),
                    Convert.ToBase64String(rsaparameters.DP),
                    Convert.ToBase64String(rsaparameters.DQ),
                    Convert.ToBase64String(rsaparameters.InverseQ),
                    Convert.ToBase64String(rsaparameters.D)
                });
            }
            else {
                result = string.Format("<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent></RSAKeyValue>", Convert.ToBase64String(rsaparameters.Modulus), Convert.ToBase64String(rsaparameters.Exponent));
            }
            return result;
        }

        // Token: 0x06000099 RID: 153 RVA: 0x0000777C File Offset: 0x0000597C
        public static string ToPemString(this X509Certificate2 cert) {
            return string.Join(RsaExtends.NEW_LINE, new string[]
            {
                cert.ToPemCertString(),
                (cert.PrivateKey as RSA).ExportParameters(true).ToPemPrivateKeyString()
            });
        }

        // Token: 0x0600009A RID: 154 RVA: 0x000077C0 File Offset: 0x000059C0
        public static string ToPemCertString(this X509Certificate2 cert) {
            return string.Join(RsaExtends.NEW_LINE, new string[]
            {
                RsaExtends.BEGIN_CERTIFICATE,
                Convert.ToBase64String(cert.GetRawCertData(), Base64FormattingOptions.InsertLineBreaks),
                RsaExtends.END_CERTIFICATE
            });
        }

        // Token: 0x0600009B RID: 155 RVA: 0x00007800 File Offset: 0x00005A00
        public static string ToPemPrivateKeyString(this RSAParameters p) {
            string result;
            using (MemoryStream memoryStream = new MemoryStream()) {
                BinaryWriter binaryWriter = new BinaryWriter(memoryStream);
                binaryWriter.Write(48);
                using (MemoryStream memoryStream2 = new MemoryStream()) {
                    BinaryWriter stream = new BinaryWriter(memoryStream2);
                    RsaExtends.EncodeIntegerBigEndian(stream, new byte[1], true);
                    RsaExtends.EncodeIntegerBigEndian(stream, p.Modulus, true);
                    RsaExtends.EncodeIntegerBigEndian(stream, p.Exponent, true);
                    RsaExtends.EncodeIntegerBigEndian(stream, p.D, true);
                    RsaExtends.EncodeIntegerBigEndian(stream, p.P, true);
                    RsaExtends.EncodeIntegerBigEndian(stream, p.Q, true);
                    RsaExtends.EncodeIntegerBigEndian(stream, p.DP, true);
                    RsaExtends.EncodeIntegerBigEndian(stream, p.DQ, true);
                    RsaExtends.EncodeIntegerBigEndian(stream, p.InverseQ, true);
                    int num = (int)memoryStream2.Length;
                    RsaExtends.EncodeLength(binaryWriter, num);
                    binaryWriter.Write(memoryStream2.GetBuffer(), 0, num);
                }
                result = string.Join(RsaExtends.NEW_LINE, new string[]
                {
                    RsaExtends.BEGIN_RSA_PRIVATE_KEY,
                    Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length, Base64FormattingOptions.InsertLineBreaks),
                    RsaExtends.END_RSA_PRIVATE_KEY
                });
            }
            return result;
        }

        // Token: 0x0600009C RID: 156 RVA: 0x00007934 File Offset: 0x00005B34
        static RsaExtends() {
            //Class15.cwcZ3SWztHroi();
            RsaExtends.FromPemStringRegex = "^(?:-----BEGIN (?:(?<r>RSA) )?(?<p>PUBLIC|PRIVATE) KEY-----)(?:\\n)*(?<k>(.|(?:\\n))*)(?:\\n)*(?:-----END (?:\\<r> )?(?:\\<p>) KEY-----)$";
            RsaExtends.NEW_LINE = "\n";
            RsaExtends.BEGIN_CERTIFICATE = "-----BEGIN CERTIFICATE-----";
            RsaExtends.END_CERTIFICATE = "-----END CERTIFICATE-----";
            RsaExtends.BEGIN_RSA_PRIVATE_KEY = "-----BEGIN RSA PRIVATE KEY-----";
            RsaExtends.END_RSA_PRIVATE_KEY = "-----END RSA PRIVATE KEY-----";
        }

        // Token: 0x04000045 RID: 69
        private static readonly string FromPemStringRegex;

        // Token: 0x04000046 RID: 70
        private static readonly string NEW_LINE;

        // Token: 0x04000047 RID: 71
        private static readonly string BEGIN_CERTIFICATE;

        // Token: 0x04000048 RID: 72
        private static readonly string END_CERTIFICATE;

        // Token: 0x04000049 RID: 73
        private static readonly string BEGIN_RSA_PRIVATE_KEY;

        // Token: 0x0400004A RID: 74
        private static readonly string END_RSA_PRIVATE_KEY;
    }
}
